﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using SharePointSlideShow.Provider;
using SharePointSlideShow.Persistence;
using SharePointSlideShow.Model;
using Microsoft.SharePoint;
using SharePointSlideShow;
using System.Collections.Generic;
using SharePointSlideShow;
using System.Data;
using SharePointSlideShow.Utility;


namespace SlideShow.SlideShow
{
    public partial class SlideShowUserControl : UserControl
    {
        #region Properties
        public string Listname { get; set; }
        public string ThemeName { get; set; }
        public int NumberofImages { get; set; }
        public bool IncludeFolder { get; set; }
        public string FolderName { get; set; }
        public string ThumbnailSize { get; set; }
        public string Transition { get; set; }
        public string Height { get; set; }
        public string Width { get; set; }
        public string SlideShowSpeed { get; set; }
        public int PageSize { get; set; }
        public string CacheExpiry { get; set; }
        public bool SlideShow { get; set; }
        public string ColumnName { get; set; }
        public string SlideShowTitle { get; set; }
        public string ItemsperPage {get;set;}
        public string Animation{get;set;}
        public string Pause{get;set;}
        public bool skipJQuery { get; set; }
        public string Siteurl { get; set; }

        #endregion
        List<ImageInfo> Images = new List<ImageInfo>();
        private static object _lock = new object();


        protected void Page_Init(object sender, EventArgs e)
        {
              try
                {
                    ClientScriptManager scriptmgr = this.Page.ClientScript;
                    if (skipJQuery == false)
                        scriptmgr.RegisterClientScriptInclude("jquery", "/_layouts/SlideShow/Scripts/jquery-1.8.0.min.js");
                
                    if (ThemeName != null)
                    {
                        HtmlLink csslink = new HtmlLink();

                        csslink.Href = "_layouts/SlideShow/style/" + ThemeName + ".css";
                        csslink.Attributes.Add("rel", "stylesheet");
                        csslink.Attributes.Add("type", "text/css");
                        Page.Header.Controls.Add(csslink);
                    }

                    
                   
                    if (!string.IsNullOrEmpty(SlideShowTitle))
                    {
                        lcTitle.Text = SlideShowTitle;
                    }
                }

                catch (Exception ex)
                {
                    SlideShowLogging.LogErrorInULS("Error in setting the default value" + ex.Message);
                }
         
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SPListItemCollection ImagesCollection=null;
           // Images = new List<ImageInfo>();
            if( !SlideCache.Get("Images",out Images))
            {
                Images = new List<ImageInfo>();
                try
                {
                    if (NumberofImages > 0 && IncludeFolder == true)
                    {
                        ImagesCollection = Gateway.Manager.GetImages(Siteurl, Listname, NumberofImages, IncludeFolder);
                    }
                    else if (!string.IsNullOrEmpty(FolderName) && NumberofImages > 0)
                    {
                        ImagesCollection = Gateway.Manager.GetImages(Siteurl, Listname, NumberofImages, FolderName);
                    }
                    else if (!string.IsNullOrEmpty(FolderName) && IncludeFolder == true)
                    {
                        ImagesCollection = SharePointSlideShow.Gateway.Manager.GetImages(Siteurl, Listname, IncludeFolder, FolderName);
                    }

                    else if (IncludeFolder == true)
                    {
                        ImagesCollection = SharePointSlideShow.Gateway.Manager.GetImages(Siteurl, Listname, IncludeFolder);

                    }
                    else if (NumberofImages > 0)
                    {
                        ImagesCollection = SharePointSlideShow.Gateway.Manager.GetImages(Siteurl, Listname, NumberofImages);
                    }
                    else if (!string.IsNullOrEmpty(FolderName))
                    {
                        ImagesCollection = SharePointSlideShow.Gateway.Manager.GetImages(Siteurl, Listname, FolderName);
                    }
                    else if ( !string.IsNullOrEmpty(FolderName)  && NumberofImages > 0 && IncludeFolder == true)
                    {
                        ImagesCollection = SharePointSlideShow.Gateway.Manager.GetImages(Siteurl, Listname, IncludeFolder, FolderName, NumberofImages);
                    }
                    else
                    {
                        ImagesCollection = SharePointSlideShow.Gateway.Manager.GetImages(Siteurl, Listname);
                    }

                     if (ImagesCollection != null || ImagesCollection.Count > 0)
                    {

                        foreach (SPListItem item in ImagesCollection)
                        {
                            if (item.Folder == null)
                            {
                                ImageInfo imginfo = new ImageInfo();
                                if (!string.IsNullOrEmpty(ColumnName))
                                {
                                    imginfo.Title = item[ColumnName].ToString();
                                }
                                else
                                {
                                    imginfo.Title =(string) item[Constant.Title];
                                }
                                
                                string tempurl = (string)item[SPBuiltInFieldId.EncodedAbsUrl];
                                imginfo.ImageURL = tempurl;
                                imginfo.ThubmailURL = (string)item[SPBuiltInFieldId.EncodedAbsThumbnailUrl];
                                Images.Add(imginfo);
                            }
                        }
                     }
                    

                    lock (_lock)
                    {
                       SlideCache.Add(Images, "Images",Convert.ToInt16(CacheExpiry), true);
                    }
                   
                }
                catch (Exception ex)
                {
                    SlideShowLogging.LogErrorInULS("Error in Loading the images" + ex.Message);
                }
            }
            this.ListViewImages.DataSource = Images;
            this.ListViewImages.DataBind(); 

        }
                
            
        
        

    }
}
